<!DOCTYPE html>
<html>
<head>
<!--
  Copyright (c) 2015-2018 Jean-Marc VIGLINO, 
  released under CeCILL-B (french BSD like) licence: http://www.cecill.info/
-->
  <title>ol-ext: Elevation layer</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <meta name="description" content="[ol-ext] Elevation map" />
  <meta name="keywords" content="ol, openlayers, layer, elevation, altitude, geoportail" />

  <!-- jQuery -->
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>

  <!-- Openlayers -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@latest/ol.css" />
  <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/ol@latest/dist/ol.js"></script>

  
  <!-- ol-ext -->
  <link rel="stylesheet" href="../../dist/ol-ext.css" />
  <script type="text/javascript" src="../../dist/ol-ext.js"></script>
  <!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
  <script src="https://unpkg.com/elm-pep"></script>

  <!-- filesaver-js -->
  <script type="text/javascript" src="https://cdn.rawgit.com/eligrey/FileSaver.js/aa9f4e0e/FileSaver.min.js"></script>

  <link rel="stylesheet" href="../style.css" />
  <style>
    pre {
      padding: .2em 1em;
      margin: .2em 1em;
      background: #ddd;
      width: auto;
      display: inline-block;
    }
  </style>
</head>
<body >
  <a href="https://github.com/Viglino/ol-ext" class="icss-github-corner"><i></i></a>

  <a href="../../index.html">
    <h1>ol-ext: x-bil elevation layer</h1>
  </a>
  <div class="info">
    This example use a TileWMS layer with an x-bil (Band Interleaved by Line) image format and a 
    <a href="https://openlayers.org/en/latest/apidoc/module-ol_Tile.html#~LoadFunction">tileLoadFunction</a>
    to encode altitude as RGB pixels.
    <br/>
    The following equation will decode pixel values to height values:
    <br/>
    <pre>height = -12000 + ((R * 256 * 256 + G * 256 + B) * 0.01)</pre>
    <br/>
    It ensure a 2 digit precision and a maximum deep watter trench up to -12000 m.
    <br/>
    Use the <i>ol.ext.getElevationFromPixel</i> function to get elevation from RGB pixel value.
  </div>

  <!-- DIV pour la carte -->
  <div id="map" style="width: 100%; height: 600px;"></div>
  <label>
    <input type="checkbox" onchange="hide.setDisplay(this.checked)"/>
    display elevation map
  </label>

<script>

var plan = new ol.layer.Geoportail({
  layer: 'GEOGRAPHICALGRIDSYSTEMS.PLANIGNV2',
  className: 'plan',
});

// The map
var map = new ol.Map ({
  target: 'map',
  view: new ol.View ({
    zoom: 0,
    center: [951487, 3467896]
  }),
  layers: [ plan ]
});
map.addControl(new ol.control.LayerSwitcher());
map.addControl(new ol.control.Permalink({ visible: false }));
map.addControl(new ol.control.SearchNominatim({ zoomOnSelect: 16 }));

// A set of elevation layers
var layers = [
  {
    title: 'MNT SRTM3',
    url: 'https://data.geopf.fr/wms-r/wms',
    layer: 'ELEVATION.ELEVATIONGRIDCOVERAGE.SRTM3',
    //extent: [ -20037554.725947514, -8625918.87376409, 20037554.725947514, 8625918.87376409 ]
  },{
    title: 'MNS',
    url: 'https://data.geopf.fr/wms-r/wms',
    layer: 'ELEVATION.ELEVATIONGRIDCOVERAGE.HIGHRES.MNS',
    extent: [ -578959.605490584, 5203133.393641367, 921974.2487313666, 6643289.75487211 ]
  }, {
    title: 'MNT-RGE-Alti',
    url: 'https://data.geopf.fr/wms-r/wms',
    layer: 'ELEVATION.ELEVATIONGRIDCOVERAGE.HIGHRES',
    extent: [ -7007874.496280316, -1460624.494037931, 5043253.3127169, 6639937.650114076 ]
  }, {
    title: 'MNT BDAlti V1',
    url: 'https://data.geopf.fr/wms-r/wms',
    layer: 'ELEVATION.ELEVATIONGRIDCOVERAGE',
    extent: [ -7007874.496280316, -1460624.494037931, 5043253.3127169, 6639937.650114076 ]
  }
];

// Add tile layer
var layer = layers[0]
var elev = new ol.layer.Tile ({
  title: layer.title,
  displayInLayerSwitcher: false,
  extent: layer.extent,
  minResolution: 0,
  maxResolution: 197231.79878968254,
  source: new ol.source.TileWMS({
    url: layer.url,
    projection: 'EPSG:3857',
    attributions: [ 'Geoservices-IGN' ],
    crossOrigin: 'anonymous',
    params: {
      LAYERS: layer.layer,
      FORMAT: 'image/x-bil;bits=32',
      VERSION: '1.3.0'
    }
  })
});
map.addLayer(elev);

// Tile load function to convert elevation
var alti = ol.ext.imageLoader.elevationMap();
elev.getSource().setTileLoadFunction(alti);

// Hide the layer (but keep it on the map)
var hide = new ol.filter.CSS({ display: false });
elev.addFilter(hide);

// Prevent layer smoothing
elev.once('prerender', function(evt) {
  evt.context.imageSmoothingEnabled = false;
  evt.context.webkitImageSmoothingEnabled = false;
  evt.context.mozImageSmoothingEnabled = false;
  evt.context.msImageSmoothingEnabled = false;
});

// Add a popup to display elevation
var popup = new ol.Overlay.Tooltip();
map.addOverlay(popup)
map.on('pointermove', function(e) {
  var pix = elev.getData(e.pixel);
  var h = ol.ext.getElevationFromPixel(pix);
  popup.setInfo(h > -5000 ? h.toFixed(2)+' m' : '');
});

</script>
</body>
</html>